package com.xiaoxiang.mall.controller;


import com.xiaoxiang.common.result.HttpResult;
import com.xiaoxiang.mall.entity.ShoppingCart;
import com.xiaoxiang.mall.service.ShoppingCartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (ShoppingCart)表控制层
 *
 * @author haha
 * @since 2022-03-29 13:23:08
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    /**
     * 服务对象
     */
    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 查询所有数据
     */
    @GetMapping("list")
    public HttpResult selectAll(@RequestParam Long userId) {
        return HttpResult.success(this.shoppingCartService.getAllByUserId(userId));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping
    public HttpResult selectOne(@RequestParam Long id) {
        return HttpResult.success(this.shoppingCartService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param shoppingCart 实体对象
     * @return 新增结果
     */
    @PostMapping
    public HttpResult insert(@RequestBody ShoppingCart shoppingCart) {
        return HttpResult.success(this.shoppingCartService.save(shoppingCart));
    }

    /**
     * 修改数据
     *
     * @param shoppingCart 实体对象
     * @return 修改结果
     */
    @PutMapping
    public HttpResult update(@RequestBody ShoppingCart shoppingCart) {
        return HttpResult.success(this.shoppingCartService.updateById(shoppingCart));
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping
    public HttpResult delete(@RequestParam("id") Long id) {
        return HttpResult.success(this.shoppingCartService.removeById(id));
    }
}
